package com.example.springboot.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.springboot.Result;
import com.example.springboot.domain.User;
import com.example.springboot.enums.ResultCodeEnum;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;

//用户登录页面
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping("/")
    public Result hello(){
        return Result.success("访问成功");
    }

//    登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if(ObjectUtil.isEmpty(user.getUsername()) || ObjectUtil.isEmpty(user.getPassword())){
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        else {
            user = userService.login(user);
        }
        return Result.success(user);
    }

//    修改密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())
            || ObjectUtil.isEmpty(user.getNewPassword())){
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR2);
        }
        else {
            userService.updatePassword(user);
        }
        return Result.success();
    }

//    注册账号
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        else{
            userService.register(user);
        }
        return Result.success();
    }
}
